package com.neu.niuyuchao.servlet;

import com.neu.niuyuchao.service.MgrInfoService;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.Map;

@WebServlet(name = "RegServlet", urlPatterns = {"/RegServlet.do"})
@MultipartConfig
public class RegServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Part part=request.getPart("mgr_img");
        String path=request.getServletContext().getRealPath("img");
        //System.out.println(part.getHeaderNames());
        String fileName=part.getHeader("content-disposition");
        String name=fileName.substring(fileName.lastIndexOf("."),fileName.length()-1);
        name=System.currentTimeMillis()+name;

        part.write(path+"/"+name);
        Map<String,String[]> map=request.getParameterMap();

        MgrInfoService mgrService=new MgrInfoService();
        boolean flag=mgrService.doSave(map,name);
        if(flag){
            response.sendRedirect(request.getContextPath()+"/commons/success.jsp");
        }else{
            response.sendRedirect(request.getContextPath()+"/commons/fail.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
